from db import Db


class Acl:
    def __init__(self):
        self.db = Db()

    # 添加权限
    def add_resource(self, uid, resourceid):
        sql = "insert into user_resource(userid,resourceid) values (%s,%s)" % (uid, resourceid)
        self.db.update(sql)

    #
    # 修改权限
    def update_resource(self, uid, rid):
        sql = "delete from user_resource where userid=%d" % (int(uid))
        self.db.update(sql)
        self.add_resource(uid, rid)

    #
    #
    #     #删除单个权限
    def delete_resource(self, uid, resourceid):
        sql = "delete from user_resource where id=%d and resourceid=%d" % (int(uid), int(resourceid))
        self.db.update(sql)

    #
    # 获取页面资源
    def get_pagedef_reslist(self, uid):
        sql = "select * from user_resource as u inner join resources as r on u.resourceid = r.id where u.userid=%d and r.type=1" % (
            int(uid))
        res = self.db.findall(sql)
        list1 = []
        for i in res:
            list1.append(i['url'])
        return list1

    #
    #
    #
    #     #获取接口资源
    def get_interface_reslist(self, uid):
        sql = "select * from user_resource as u inner join resources as r on u.resourceid = r.id where u.userid=%d and r.type=2" % (
            int(uid))
        res = self.db.findall(sql)
        list1 = []
        for i in res:
            list1.append(i['url'])
        return list1
#
acl = Acl()
# acl.add_resource(1,2)
# print(acl.get_interface_reslist(1))
#
#
#
#
#
